/*******************************************************************************
 * @brief: lcd driver
 * @Author:   aple
 * @Date: 2022-12-03 20:15:45
 * @LastEditors: aple
 * @LastEditTime: 2022-12-03 20:33:10
 * @Verision: 1.0.0.0
 ******************************************************************************/
/*******************************************************************************
 *	                           bsp cpu.c
 *******************************************************************************/
#include "bsp.h"
#include "framework.h"
#include "lib_surport.h"

#define delay(x) bsp_delay_ms(x)
#define delay_us(x) bsp_delay_us(x)

extern swiic_cfg_type bsp_sw_iic;
#define iicx (&bsp_sw_iic)
#define OLED_CMD 0	// 写命令
#define OLED_DATA 1 // 写数据
#define USE_HORIZONTAL 90
void OLED_Clear(void);

// 写指令到 LCD 模块
void lcd_transfer_command(uint8_t dat, uint8_t mode)
{
	sw_hiic_start(iicx);

	sw_hiic_send_byte(iicx, 0x78);

	if (mode)
	{
		sw_hiic_send_byte(iicx, 0x40);
	}
	else
	{
		sw_hiic_send_byte(iicx, 0x00);
	}

	sw_hiic_send_byte(iicx, dat);

	sw_hiic_stop(iicx);
}

// 写数据到 LCD 模块
void lcd_transfer_data(int lcd, int data1)
{
}

// 写数据到 LCD 模块
void lcd_transfer_str(int lcd, uint8_t *data, int len)
{
}

/*******************************************************************************
 * @brief: initial_lcd
 * @param {*}
 * @return {*}
 ******************************************************************************/
void lcd_hal_init(void)
{
	lcd_transfer_command(0xAE, OLED_CMD); //--turn off oled panel

	lcd_transfer_command(0xA8, OLED_CMD); /*multiplex ratio*/
	lcd_transfer_command(0x3F, OLED_CMD); /*duty = 1/64*/

	lcd_transfer_command(0xAD, OLED_CMD);
	lcd_transfer_command(0x40, OLED_CMD);

	lcd_transfer_command(0xD3, OLED_CMD); /*set display offset*/
	lcd_transfer_command(0x00, OLED_CMD); /*   0x20   */

	lcd_transfer_command(0xA4, OLED_CMD); /*Disable Entire Display On (0xA4/0xA5)*/

	lcd_transfer_command(0xA6, OLED_CMD); /*normal / reverse*/
	lcd_transfer_command(0x40, OLED_CMD);
	lcd_transfer_command(0x81, OLED_CMD); /*contract control*/
	lcd_transfer_command(0x57, OLED_CMD); /*128*/

	lcd_transfer_command(0xD9, OLED_CMD); /*set pre-charge period*/
	lcd_transfer_command(0x42, OLED_CMD);

	lcd_transfer_command(0xD5, OLED_CMD);
	lcd_transfer_command(0x90, OLED_CMD);

	lcd_transfer_command(0xDA, OLED_CMD); /* Set SEG Pins Hardware Configuration */
	lcd_transfer_command(0x10, OLED_CMD);

	if (USE_HORIZONTAL == 0)
	{
		lcd_transfer_command(0x20, OLED_CMD); /* Set Memory addressing mode (0x20/0x21) */
		lcd_transfer_command(0x09, OLED_CMD); /* 0x09 */

		lcd_transfer_command(0xA1, OLED_CMD); /*set segment remap  0XA1 */
		lcd_transfer_command(0xC8, OLED_CMD); /*Com scan direction   0Xc8  */
	}
	else if (USE_HORIZONTAL == 90)
	{
		lcd_transfer_command(0x20, OLED_CMD); /* Set Memory addressing mode (0x20/0x21) */
		lcd_transfer_command(0x02, OLED_CMD); /* 0x02 */

		lcd_transfer_command(0xA1, OLED_CMD); /*set segment remap  0XA1 */
		lcd_transfer_command(0xC0, OLED_CMD); /*Com scan direction   0Xc0  */
	}
	else if (USE_HORIZONTAL == 180)
	{
		lcd_transfer_command(0x20, OLED_CMD); /* Set Memory addressing mode (0x20/0x21) */
		lcd_transfer_command(0x09, OLED_CMD); /* 0x09 */

		lcd_transfer_command(0xA0, OLED_CMD); /*set segment remap  0XA0 */
		lcd_transfer_command(0xC0, OLED_CMD); /*Com scan direction   0Xc0  */
	}
	else if (USE_HORIZONTAL == 270)
	{
		lcd_transfer_command(0x20, OLED_CMD); /* Set Memory addressing mode (0x20/0x21) */
		lcd_transfer_command(0x02, OLED_CMD); /* 0x02 */

		lcd_transfer_command(0xA0, OLED_CMD); /*set segment remap  0XA0 */
		lcd_transfer_command(0xC8, OLED_CMD); /*Com scan direction   0Xc8  */
	}
	lcd_transfer_command(0xdb, OLED_CMD); /*set vcomh*/
	lcd_transfer_command(0x30, OLED_CMD);

	lcd_transfer_command(0x8d, OLED_CMD); /*set charge pump enable*/
	lcd_transfer_command(0x14, OLED_CMD);

	lcd_transfer_command(0x00, OLED_CMD); /*set lower column address*/
	lcd_transfer_command(0x10, OLED_CMD); /*set higher column address*/

	OLED_Clear();
	lcd_transfer_command(0xAF, OLED_CMD);
}

void OLED_Refresh(void)
{
	u8 i, n;
	for (i = 0; i < 8; i++)
	{
		lcd_transfer_command(0xb0 + i, OLED_CMD);
		lcd_transfer_command(0x00, OLED_CMD);
		lcd_transfer_command(0x10, OLED_CMD);
		sw_hiic_start(iicx);
		;
		sw_hiic_send_byte(iicx, 0x78);

		sw_hiic_send_byte(iicx, 0x40);
		for (n = 0; n < 128; n++)
		{
			sw_hiic_send_byte(iicx, 0xA3);
		}
		sw_hiic_stop(iicx);
	}
}

void OLED_Clear(void)
{
	u8 i, n;
	for (i = 0; i < 8; i++)
	{
		lcd_transfer_command(0xb0 + i, OLED_CMD);
		lcd_transfer_command(0x00, OLED_CMD);
		lcd_transfer_command(0x10, OLED_CMD);
		
		sw_hiic_start(iicx);
		sw_hiic_send_byte(iicx, 0x78);
		sw_hiic_send_byte(iicx, 0x40);
		for (n = 0; n < 128; n++)
		{
			sw_hiic_send_byte(iicx, 0xCC);
		}
		sw_hiic_stop(iicx);
	}
}

void lcd_data_write_ext(char page, uu8 *pLineUp, uu8 *pLineDn, uu8 offset)
{

}

void lcd_data_write(char page, uint8_t *pdata)
{
	u8 i = page;
	u8 n;

		lcd_transfer_command(0xb0 + i, OLED_CMD);
		lcd_transfer_command(0x00, OLED_CMD);
		lcd_transfer_command(0x10, OLED_CMD);
		
		sw_hiic_start(iicx);
		sw_hiic_send_byte(iicx, 0x78);
		sw_hiic_send_byte(iicx, 0x40);
		for (n = 0; n < 128; n++)
		{
			sw_hiic_send_byte(iicx, pdata[n]);
		}
		sw_hiic_stop(iicx);
}


/*写 LCD 行列地址：X 为起始的列地址 */
void lcd_address(int lcd, int page)
{
	lcd_transfer_command(lcd, 0xb0 + page);
	lcd_transfer_command(lcd, 0x10);
	lcd_transfer_command(lcd, 0x00);
}
